package com.javaweb.demo.servlet;

import com.javaweb.demo.dao.UserDao;
import com.javaweb.demo.dao.UserDaolmpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

public class DownloadServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String fileid = request.getParameter("id");
        int id = Integer.parseInt(fileid);
        UserDao ud = new UserDaolmpl();
        String username = request.getParameter("username");
        request.setAttribute("username",username);
        try {
            if(ud.download(id)) {
                if (username.equals("daikunqi")) {
                    request.getRequestDispatcher("/successM.jsp").forward(request, response);
                }
                else {
                    request.getRequestDispatcher("/success.jsp").forward(request, response);
                }
            }
            else {
                request.getRequestDispatcher("/fail.jsp").forward(request, response);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }




}


